package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Util.JdbcUtil;
import Entity.Bill_Entity;
import Entity.User_List_Entity;

public class Bill_List_Dao {
//	查
	public List<Bill_Entity>select(){
		List<Bill_Entity>list = new ArrayList<Bill_Entity>();
		try {
			Connection conn = JdbcUtil.getConnection();
			String sql = "select * from bill;";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				Bill_Entity bills = new Bill_Entity();
				bills.setId(rs.getInt(1));
				bills.setName(rs.getString(2));
				list.add(bills);
			}
			JdbcUtil.close(pstmt, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
//	改
	public void update(int id,String name){
		try {
			Connection conn = JdbcUtil.getConnection();
			String sql = "update bill set name='"+ name +"' where id="+ id +";";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.executeUpdate();
			JdbcUtil.close(pstmt, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
//	删
	public void delete(int id){
		try {
			Connection conn = JdbcUtil.getConnection();
			String sql = "delete from bill where id = "+ id +";";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.executeUpdate();
			JdbcUtil.close(pstmt, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
//	增
	public void add(String name){
		try {
			Connection conn = JdbcUtil.getConnection();
			String sql = "insert into bill(name) value('"+name+"');";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.executeUpdate();
			JdbcUtil.close(pstmt, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
